package com.axungu.web.controller;

import com.axungu.common.Result;
import com.axungu.common.ServletContext;
import com.axungu.common.paginator.domain.Order;
import com.axungu.common.paginator.domain.PageBounds;
import com.axungu.common.paginator.domain.PageResult;
import com.axungu.core.modal.ssh.HostAuth;
import com.axungu.core.modal.ssh.HostGroup;
import com.axungu.core.modal.ssh.HostInfo;
import com.axungu.core.oauth.Permission;
import com.axungu.core.service.HostAuthService;
import com.axungu.core.service.HostGroupService;
import com.axungu.core.service.HostInfoService;
import com.axungu.core.vo.ssh.HostInfoVO;
import com.axungu.exception.NoFoundException;
import com.axungu.exception.ServiceException;
import com.axungu.web.params.SaveHostInfoParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * @author jtoms.shen
 * @version 1.0
 * @date 2018/12/7 17:38
 */
@Slf4j
@Controller
@RequestMapping("/host/info")
public class HostInfoController {

    @Autowired
    private HostInfoService hostInfoService;

    @Autowired
    private HostAuthService hostAuthService;

    @Autowired
    private HostGroupService hostGroupService;

    @Permission
    @GetMapping("/index.htm")
    public String index(HostInfoVO vo, ModelMap modelMap) {

        PageBounds bounds = new PageBounds(vo.getPage(), vo.getLimit());

        if (StringUtils.isBlank(vo.getOrders())) {
            bounds.addOrder(Order.create("id", Order.Direction.DESC));
        } else {
            bounds.setOrders(Order.formString(vo.getOrders()));
        }

        String accessToken = ServletContext.getAccessToken();

        PageResult<HostInfo> pageResult = hostInfoService.findPage(bounds,vo);

        List<HostInfo> listData = pageResult.getData();
        for (HostInfo hostInfo : listData) {
            hostInfo.setHostGroup(this.hostGroupService.findById(hostInfo.getGroupId()));
            hostInfo.setHostAuth(this.hostAuthService.findById(hostInfo.getAuthId()));
        }
        modelMap.addAttribute("listData", pageResult.getData());
        modelMap.addAttribute("paginator", pageResult.getPaginator());
        modelMap.addAttribute("accessToken", accessToken);
        modelMap.addAttribute("vo", vo);

        List<HostAuth> listAuths = this.hostAuthService.findAll();
        modelMap.addAttribute("listAuths", listAuths);
        List<HostGroup> listGroups = this.hostGroupService.findAll();
        modelMap.addAttribute("listGroups", listGroups);

        return "host/info/index";
    }

    @Permission
    @GetMapping("/edit.htm")
    public String edit(Long id, ModelMap modelMap) throws NoFoundException {
        HostInfo hostInfo = new HostInfo();
        if (id != null) {
            hostInfo = this.hostInfoService.findById(id);
        }
        if (hostInfo == null) {
            throw new NoFoundException();
        }
        modelMap.addAttribute("hostInfo", hostInfo);

        List<HostAuth> listAuths = this.hostAuthService.findAll();
        modelMap.addAttribute("listAuths", listAuths);
        List<HostGroup> listGroups = this.hostGroupService.findAll();
        modelMap.addAttribute("listGroups", listGroups);

        return "host/info/edit";
    }

    @Permission
    @ResponseBody
    @PostMapping("/insertOrUpdate.ajax")
    public Result insertOrUpdate(Long id, @Valid SaveHostInfoParam param) throws ServiceException {

        HostInfo hostInfo = new HostInfo();
        if (id != null && id.compareTo(0L) > 0) {
            hostInfo.setId(id);
        }
        hostInfo.setGroupId(param.getGroupId());
        hostInfo.setAuthId(param.getAuthId());
        hostInfo.setName(param.getName());
        hostInfo.setHostAddress(param.getHostAddress());
        hostInfo.setPortNumber(param.getPortNumber());
        hostInfo.setOs(param.getOs());
        hostInfo.setRemark(param.getRemark());

        this.hostInfoService.insertOrUpdate(hostInfo);
        return Result.instance(Result.Code.SUCCESS);
    }


    @Permission
    @ResponseBody
    @PostMapping("/delete.ajax")
    public Result delete(Long[] ids) throws ServiceException {
        this.hostInfoService.delete(ids);
        return Result.instance(Result.Code.SUCCESS);
    }
}
